package org.miframework.arqsw.frontcontroller;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.catalina.tribes.group.interceptors.TwoPhaseCommitInterceptor.MapEntry;
import org.miframework.arqsw.VistaMap.VistaMap;
import org.miframework.arqsw.controller.Controlador;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.lang.reflect.*;

public class FrontController extends HttpServlet {
	private Map<String, Controlador> controladores = new HashMap<String, Controlador>();
	private Map<String, Controlador> controladoresA = new HashMap<String, Controlador>();
	private String config;
	
	public FrontController() {
		// TODO Auto-generated constructor stub
		config="config.xml";
	}
	public FrontController(String config) {
		// TODO Auto-generated constructor stub
		this.config = config;
	}
    @Override
    public void init() throws ServletException{
		controladores= new ControllerMap().mapear(getServletConfig().getServletContext().getRealPath("WEB-INF//"+config));
		try {
			controladoresA = new AnnotationParser().mapear("C:"+(char)92+"Users"+(char)92+"Pablo"+(char)92+"workspace"+(char)92+"miframework"+(char)92+"src"+(char)92+"com"+(char)92+"prueba"+(char)92+"myapp");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    }
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    	String sp = request.getServletPath();
    	String resultado="";
        System.out.println("servlet path = " + sp);
        Controlador con = (Controlador)controladores.get(sp);
        if(con!=null){        	
        	System.out.println(con.run());
        	VistaMap vm = new VistaMap(sp, con.run());
        	resultado=vm.mapeo();
        }
        else{
        	con = (Controlador)controladoresA.get(sp);
        	if(con!=null){        	
            	System.out.println(con.run());
            	VistaMap vm = new VistaMap(sp, con.run());
            	resultado=vm.mapeo();
            }
        }
        //request.getRequestDispatcher("/index.jsp").forward(request, response);
        request.getRequestDispatcher("/"+resultado+".jsp").forward(request, response);
     	   
    }


}
